﻿using FileSC.Configs.StatusBar;
using FileSC.FileSystem;
using FileSC.WEBService.Client;
using FileSC.WEBService.Dto;
using FileSC.WEBService.Job;
using FileSC.WinAPI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Http;

namespace FileSC.WEBService.Plugins
{
    internal class StoragePlug : PluginBase, IHttpPlugin
    {
        public async Task OnHttpRequest(IHttpSessionClient client, HttpContextEventArgs e)
        {
            var request = e.Context.Request;//http请求体
            var response = e.Context.Response;//http响应体
            if (request.IsPost())
            {
                try
                {
                    switch (request.RelativeURL)
                    {
                        case "/storage/open":
                            var data = await request.GetDataAsync<DownloadFileInput>();
                            StorageJob.OpenFile(data);
                            await response
                                    .SetStatus(200, "success")
                                    .FromText("Success")
                                    .AnswerAsync();//直接回应
                            return;
                        case "/storage/download":
                            var data2 = await request.GetDataAsync<DownloadFileInput>();

                            StorageJob.DownloadFile(data2);
                            await response
                                    .SetStatus(200, "success")
                                    .FromText("Success")
                                    .AnswerAsync();//直接回应
                            return;
                        case "/storage/upload/file":
                            var data3 = await request.GetDataAsync<UploadFileInput>();

                            StorageJob.UploadFile(data3);
                            await response
                                    .SetStatus(200, "success")
                                    .FromText("Success")
                                    .AnswerAsync();//直接回应
                            return;
                        case "/storage/upload/path":
                            var data4 = await request.GetDataAsync<UploadPathInput>();

                            StorageJob.UploadPath(data4);
                            await response
                                    .SetStatus(200, "success")
                                    .FromText("Success")
                                    .AnswerAsync();//直接回应
                            return;
                        case "/storage/pick/file":
                            var pickfile_path = FilePicker.Choose();

                            if (string.IsNullOrEmpty(pickfile_path))
                            {
                                //未选择
                                await response
                                        .SetStatus(200, "success")
                                        .FromObj(new
                                        {
                                            name = string.Empty,
                                            path = string.Empty,
                                        })
                                        .AnswerAsync();
                                return;
                            }
                            await response
                                    .SetStatus(200, "success")
                                    .FromObj(new
                                    {
                                        name = Path.GetFileName(pickfile_path),
                                        path = pickfile_path,
                                    })
                                    .AnswerAsync();
                            return;
                        case "/storage/pick/path":
                            var pickpath_path = DirectoryPicker.Choose();
                            if (string.IsNullOrEmpty(pickpath_path))
                            {
                                //未选择
                                await response
                                        .SetStatus(200, "success")
                                        .FromObj(new
                                        {
                                            name = string.Empty,
                                            path = string.Empty,
                                        })
                                        .AnswerAsync();
                                return;
                            }
                            await response
                                    .SetStatus(200, "success")
                                    .FromObj(new {
                                        name = Path.GetFileName(pickpath_path),
                                        path = pickpath_path,
                                    })
                                    .AnswerAsync();
                            return;
                    }
                }
                catch
                {
                    StatusBar.SetIcon(IconType.Error);
                    throw;
                }
            }
            await e.InvokeNext();
        }
    }
}
